package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.listener.ExcelRespProp;
import com.TeacherInfoServer.listener.PositionHistoryListener;
import com.TeacherInfoServer.pojo.DTO.PositionHistoryDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.PositionHistorySearchDTO;
import com.TeacherInfoServer.pojo.excelImport.PositionHistoryExcel;
import com.TeacherInfoServer.common.service.PositionHistoryService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/s1/positionHistory")
@Tag(name = "历任主管任职信息控制器",description = "用于对历任主官任职信息的控制")
public class PositionHistoryController {
    @Autowired
    public PositionHistoryService positionHistoryService;
    @Autowired
    public PositionHistoryListener positionHistoryListener;
    @Operation(summary = "获取历任主官任职信息列表",description = "分页获取历任主官任职信息列表")
    @GetMapping("/getList")
    public Result<Map<String,Object>> getAllList(@RequestParam("pages") @Parameter(description = "当前页数") Integer pages,
                                                 @RequestParam("size") @Parameter(description = "当前页数显示信息的数量") Integer size){
        return Result.success("获取历任主官信息列表成功",positionHistoryService.getPositionList(pages,size));
    }
    @Operation(summary = "模糊查询获取历任主官信息列表",description = "模糊查询分页获取历任主官经历信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody PositionHistorySearchDTO positionHistorySearchDTO){
        return Result.success("获取历任主官列表成功",positionHistoryService.searchPositionHistoryList(positionHistorySearchDTO));
    }
    @Operation(summary = "添加单个任职主官信息",description = "添加单个任职主官信息")
    @PostMapping("/addOne")
    public Result<?> addOne(@RequestParam("departmentName") @Parameter(description = "教研室名称") String departmentName,
                                @RequestParam("teacherName") @Parameter(description = "任职人姓名") String teacherName,
                                @RequestParam("positionType") @Parameter(description = "任职类型") String positionType,
                                @RequestParam("startDate") @Parameter(description = "起始日期") String startDate,
                                @RequestParam("endDate") @Parameter(description = "终止日期") String endDate){
        positionHistoryService.addPositionHistory(departmentName,teacherName,startDate,endDate,positionType);
        return Result.success("添加任职主官信息成功");
    }

    @Operation(summary = "更新单个任职主官信息",description = "更新单个任职主官信息")
    @PutMapping("/updateOne")
    public Result<?> updateOne(@RequestBody PositionHistoryDTO positionHistoryDTO){
        positionHistoryService.updatePositionHistory(positionHistoryDTO);
        return Result.success("修改任职主官信息成功");
    }

    @Operation(summary = "根据id批量删除任职信息",description = "根据id批量删除任职信息")
    @DeleteMapping("/deletePositionHistory")
    public Result<?> deleteDepartment(@RequestParam("ids") @Parameter(description = "选择删除的任职信息id列表") List<Long> ids){
        boolean isDelete=positionHistoryService.deletePositionHistory(ids);
        if (isDelete) {
            return Result.success("删除成功");
        }
        else {
            return Result.fail("没有相应任职信息，删除失败");
        }
    }

    @Operation(summary = "Excel批量导入历任专业主官任职信息",description = "Excel批量导入历任专业主官任职信息")
    @PostMapping("/addByBatch")
    @Transactional
    public Result<Map<String,Integer>> addBatch(@RequestParam("file")@Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {
        positionHistoryListener.initList();
        EasyExcel.read(file.getInputStream(), PositionHistoryExcel.class,positionHistoryListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",positionHistoryListener.getRecord());
        return Result.success(200,"批量导入成功",map);
    }

    @Operation(summary = "Excel导出历任专业主官任职信息",description = "Excel导出历任专业主官任职信息")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response){
        ExcelRespProp.setExcelRespProp(response, "历任专业主官信息列表");
        List<PositionHistoryDTO> positionHistoryDTOS = positionHistoryService.getPositionHistoryDTOS();
        EasyExcel.write(response.getOutputStream())
                .head(PositionHistoryDTO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("历任专业主官任职信息列表")
                .doWrite(positionHistoryDTOS);

    }

}
